package cn.virens.config;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.spring.ProcessEngineFactoryBean;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.transaction.PlatformTransactionManager;

import com.alibaba.druid.pool.DruidDataSource;

import cn.virens.web.components.activiti.ActivitiServiceFactoryBean;

/**
 * 工作流配置
 * 
 * @作者 :loioi
 * @创建时间 :2017年2月6日
 */
@Order(3)
@Configuration
@ComponentScan("cn.loioi.web.components.activiti.listener")
public class SpringActivitiConfig {
	@Autowired
	private DruidDataSource druidDataSource;

	@Autowired
	private PlatformTransactionManager transactionManager;

	@Bean("process-engine-configuration")
	public SpringProcessEngineConfiguration configuration() {
		SpringProcessEngineConfiguration configuration = new SpringProcessEngineConfiguration();
		configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
		configuration.setTransactionManager(transactionManager);
		configuration.setDataSource(druidDataSource);
		configuration.setAsyncExecutorActivate(true);
		return configuration;
	}

	@Bean("process-engine")
	public ProcessEngineFactoryBean factoryBean(@Qualifier("process-engine-configuration") SpringProcessEngineConfiguration configuration) {
		ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
		factoryBean.setProcessEngineConfiguration(configuration);

		return factoryBean;
	}

	@Bean("activiti-service")
	public ActivitiServiceFactoryBean identityService(@Qualifier("process-engine") ProcessEngine processEngine) throws Exception {
		return new ActivitiServiceFactoryBean(processEngine);
	}
}